package orderoid.soap.business;

import orderoid.soap.Tool.DeserialisationTool;
import orderoid.soap.classeCommune.Client;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.util.Log;

public class webServiceAuthentification extends AsyncTask<String, Void, Client> {

	private static final String PARAM_LOGIN = "login";
	private static final String PARAM_PASSWORD = "password";
	
	@Override
	protected Client doInBackground(String... params) {
		String login = params[0];
		String mdp = params[1];
		Client client = new Client();

		Log.e("Test", "doInBackground : WebService Authentification");

		SoapObject request = new SoapObject(
				orderoid.soap.constant.method.NAMESPACE,
				orderoid.soap.constant.method.METHOD_NAME_AUTHENTIFICATION);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		
		envelope.setOutputSoapObject(request);
		
		HttpTransportSE androidHttpTransport = new HttpTransportSE(
				orderoid.soap.constant.method.URL);

		request.addProperty(PARAM_LOGIN, login);
		request.addProperty(PARAM_PASSWORD, mdp);

		try {
			androidHttpTransport.call(orderoid.soap.constant.method.SOAP_ACTION_AUTHENTIFICATION, envelope);
			SoapObject soapObjectResp = (SoapObject) envelope.getResponse();
			
			client = DeserialisationTool.deserialisationClient(soapObjectResp);
			
		} catch (Exception e) {
			Log.e("Erreur",e.getMessage());
			client = null;
		}
		return client;
	}

}
